package com.sprouting.ops.service;


import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sprouting.ops.dao.DromaraFileStorageDao;
import com.sprouting.ops.dao.OpsKnowledgeCommentDao;
import com.sprouting.ops.dao.OpsKnowledgeDao;
import com.sprouting.ops.interfaces.ConstantBase;
import com.sprouting.ops.model.DromaraFileStorage;
import com.sprouting.ops.model.OpsDeployLink;
import com.sprouting.ops.model.OpsKnowledge;
import com.sprouting.ops.model.OpsKnowledgeComment;
import com.sprouting.ops.model.qo.OpsKnowledgeQo;
import com.sprouting.ops.model.vo.OpsKnowledgeVo;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 知识中心(OpsKnowledge)(OPS_KNOWLEDGE) 表服务实现类
 *
 * @author ：lx（长沙麓谷）
 * @date 创建时间：2025-08-21 22:45:33
 */
@Service
@Slf4j
public class OpsKnowledgeService extends ServiceImpl<OpsKnowledgeDao, OpsKnowledge> {

    @Resource
    private OpsKnowledgeDao opsKnowledgeDao;
    @Resource
    private OpsKnowledgeCommentDao opsKnowledgeCommentDao;
    @Resource
    private DromaraFileStorageDao dromaraFileStorageDao;

    /**
     * 分页查询 知识中心
     * @param page 分页参数
     * @param query 查询参数
     * @return
     */
    public Page<OpsKnowledgeVo> getListPage(Page page, OpsKnowledgeQo query){
        return opsKnowledgeDao.getListPage(page, query);
    }

    /**
     * 删除知识中心
     * @param id 知识库id
     * @param bindList 图片信息
     */
    @Transactional(rollbackFor = Exception.class)
    public void delOpsKnowledge(String id, List<DromaraFileStorage> bindList) {
        opsKnowledgeDao.deleteById(id);
        // 删除评论
        LambdaUpdateWrapper<OpsKnowledgeComment> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(OpsKnowledgeComment::getKnowledgeId, id);
        opsKnowledgeCommentDao.delete(updateWrapper);
        // 删除图片
        if (CollUtil.isNotEmpty(bindList)){
            for (DromaraFileStorage dromaraFileStorage : bindList) {
                dromaraFileStorage.setTempFile(ConstantBase.TempFile.PRODUCTION_DEL);
                dromaraFileStorage.setDelTime(new Date());
                dromaraFileStorageDao.updateById(dromaraFileStorage);
            }
        }
    }

    /**
     * 保存知识库
     * @param bo 知识库
     * @param list 知识库内容图片
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveOpsKnowledge(OpsKnowledge bo, List<DromaraFileStorage> list) {
        opsKnowledgeDao.insert(bo);
        // md 图片
        if (CollUtil.isNotEmpty(list)){
            for (DromaraFileStorage dromaraFileStorage : list) {
                dromaraFileStorageDao.updateById(dromaraFileStorage);
            }
        }
    }

    /**
     * 更新知识中心
     * @param bo 知识库
     * @param list 知识库内容图片
     */
    @Transactional(rollbackFor = Exception.class)
    public void updOpsKnowledge(OpsKnowledge bo, List<DromaraFileStorage> list) {
        opsKnowledgeDao.updateById(bo);

        if (CollUtil.isNotEmpty(list)){
            for (DromaraFileStorage dromaraFileStorage : list) {
                dromaraFileStorageDao.updateById(dromaraFileStorage);
            }
        }
    }

    /**
     * 获取知识库-最高评分的知识库
     */
    public List<OpsKnowledge> getHomeKnowledge() {
        return opsKnowledgeDao.getHomeKnowledge();
    }
}
